#!/bin/bash


#userName=$1;
#userPwd=$2;

if  [ $# -lt 2 ] ;then
    echo "start.sh exec format:'base start.sh {userName} {userPwd}'"
    exit 1;
fi


sudo docker run -d --rm  --name=mysql bin/mysql:8.0.22 gosu mysql mysqld 

echo "mysql docker started"
cat>init_user.sql<<EOF
CREATE USER '$1'@'%' IDENTIFIED WITH sha256_password BY '$2';
GRANT ALL PRIVILEGES ON *.* TO '$1'@'%' WITH GRANT OPTION ;
FLUSH PRIVILEGES;
EOF

echo "cp sql"
sudo docker cp init_user.sql mysql:/var/lib/init_user.sql


flag=0;
while [ $flag -le 5 ];
do
	let flag++;
	# wait mysql started
	echo "exec sql, count $flag"
	sleep 3;
	sudo docker exec mysql bash -c "mysql < /var/lib/init_user.sql" && flag=6;
done
rm init_user.sql
